// low resource character fifo

#include "system.h"

// variables
byte fifobuffer[DEPTH];
int front, back, count;

// Initialize fifo
void initFifo(void)
{
	count = 0;
	front = 0;
	back = 0;
}

// insert
int fifoInsert(byte d)
{
	if (count == DEPTH) return 0;
	fifobuffer[back] = d;
	if (++back == DEPTH) back = 0;
	count++;
	return 1;
}

// remove
int fifoRemove(byte *d)
{
	if (count == 0) return 0;
	*d = fifobuffer[front];
	if (++front == DEPTH) front = 0;
	count--;
	return 1;
}

// count
int getCount(void)
{
	return count;
}

// is full?
int isFull(void)
{
	if (count == DEPTH) return 1;
	else return 0;
}

